package com.geely.file;

import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;

public class MadeFileInfo {
    @Test
    public void mainTest() {
        //字节流操作
//        FileOutputStream();
//        FileInputStream();
        //字符流操作
        writeInfo();
//        readInfo();
    }

    private static void FileOutputStream() {
        try {
            DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File("src/main/assets/fileInfo.txt"))));
            dataOutputStream.writeUTF("\"sources\":[{\"name\":\"刘同宇\",\"age\":0,\"array\":{\"https\":\"www.baidu.com\"}}]");
            dataOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void FileInputStream() {
        try {
            DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("src/main/assets/json"))));
            System.out.println(dataInputStream.readUTF());
            dataInputStream.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    private static void writeInfo() {
        try {
//            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("src/main/assets/writeInfo.txt")), "UTF-8"));
//            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D:\\bx/a.txt"));
            FileWriter fileWriter = new FileWriter("D:\\bx/a.txt");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 100; i++) {
//                bufferedWriter.write(stringBuilder.append("a").toString());
                fileWriter.write(stringBuilder.append("a").toString());
            }
//            bufferedWriter.flush();
//            bufferedWriter.close();
            fileWriter.flush();
            fileWriter.close();
            System.out.println("end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void readInfo() {
        try {
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("src/main/assets/writeInfo.txt"))));
            BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/assets/writeInfo.txt"));
            char[] chars = new char[1024];
            while (bufferedReader.read(chars) != -1) {
                System.out.println(chars);
            }
            System.out.println("end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
